angular.module("NTApp").controller('DeviceBindingCtrl', function($state, WXService, DeviceService, $stateParams, $interval) {

  WXService.getCookieData('hvacUserInfo');

  var b = this;

  var strs = $stateParams.idStr.split('-');
  var openId = WXService.getWeChat().openId;
  var rebindTimer;
  var userDetail = DeviceService.getUserDetail();

  b.showNext = true;
  b.deviceName = '';
  b.mobile = '';
  b.address = '';

  if (userDetail.deviceName) {
    b.deviceName = userDetail.deviceName;
    b.mobile = userDetail.mobile;
    b.address = userDetail.address;
  }

  if (strs.length > 1) {
    b.physicalDeviceId = strs[1];
    var sourceOpenId = strs[0];
    var deviceId = strs[2];
    var shareCode = strs[3].replace('#', '@');
    bindingSharedDevice();
  } else {
    b.physicalDeviceId = strs[0];
  }

  b.next = function() {
    if (!b.deviceName) {
      alert('请输入设备名称');
      return;
    }

    if (!b.mobile) {
      alert('请输入手机号码');
      return;
    }

    if (!b.address) {
      alert('请输入详细地址');
      return;
    }

    bindingDevice();
  };

  function bindingDevice() {
    b.showNext = false;
    DeviceService.bindingDevice(openId, b.physicalDeviceId, b.deviceName, b.mobile, b.address).then(function(result) {

      if (result == 0) {
        connectDevice();
      }
      if (result == 1) {
        alert('设备已绑定，请尝试分享绑定');
        $state.go('DevicesList');
      }
      if (result == 2) {
        alert('设备绑定成功');
        $state.go('DevicesList');
      }
      if (result == 3) {
        connectDevice();
      }
    }, function(error) {
      connectDevice();
    })
  }

  function startRebind() {
    rebindTimer = $interval(function() {
      rebind();
    }, 1000 * 10, 10);
  }

  function rebind() {
    DeviceService.bindingDevice(openId, b.physicalDeviceId, b.deviceName, b.mobile, b.address).then(function(result) {
      $interval.cancel(rebindTimer);
      if (result == 0) {
        alert('设备不在线');
      }
      if (result == 1) {
        alert('设备已绑定，请尝试分享绑定');
      }
      if (result == 2) {
        DeviceService.setUserDetail({
          deviceName: "",
          mobile: "",
          address: ""
        });
        alert('设备绑定成功');
      }
      $state.go('DevicesList');
    }, function(error) {

    })
  }

  function bindingSharedDevice() {
    b.showNext = false;
    DeviceService.bindingSharedDevice(sourceOpenId, b.physicalDeviceId, deviceId, openId, shareCode).then(function(result) {
      if (result) {
        alert('设备绑定成功');
        $state.go('DevicesList');
      } else {
        fail();
      }
    }, function(error) {
      fail();
    });
  }

  function connectDevice() {
    wx.ready(function() {
      wx.invoke('configWXDeviceWiFi', {}, function(res) {
        if (res.err_msg === 'configWXDeviceWiFi:ok') {
          startRebind();
        } else if (res.err_msg === 'configWXDeviceWiFi:cancel') {

        } else {
          fail();
        }
      })
    });
  }

  function fail() {
    DeviceService.setUserDetail({
      deviceName: b.deviceName,
      mobile: b.mobile,
      address: b.address
    });
    $state.go('DevicesList');
    alert('配置网络失败，请联系客服人员');
  }
});
